package com.example8.multi_datasource_mybatisplus.service;

import com.example8.multi_datasource_mybatisplus.entity.source.User;
import com.example8.multi_datasource_mybatisplus.entity.target.CustomerDO;
import com.example8.multi_datasource_mybatisplus.mapper.source.UserMapper;
import com.example8.multi_datasource_mybatisplus.mapper.target.CustomerMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import java.util.List;

/**
 * @Description
 * @Author lihw
 * @Date 2024/3/28 13:10
 */
@Service
@Slf4j
public class UserService {

    final UserMapper userMapper;
    final CustomerMapper customerMapper;
    private final PlatformTransactionManager primaryTransactionManager;
    private final PlatformTransactionManager secondaryTransactionManager;

    public UserService(UserMapper userMapper, CustomerMapper customerMapper, PlatformTransactionManager primaryTransactionManager, PlatformTransactionManager secondaryTransactionManager) {
        this.userMapper = userMapper;
        this.customerMapper = customerMapper;
        this.primaryTransactionManager = primaryTransactionManager;
        this.secondaryTransactionManager = secondaryTransactionManager;
    }

    /**
     * 手动事务控制案例
     * @return
     */

    public List<User> getUserList(){

        // 创建两个独立的事务定义
        DefaultTransactionDefinition defPrimary = new DefaultTransactionDefinition();
        defPrimary.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);

        DefaultTransactionDefinition defSecondary = new DefaultTransactionDefinition();
        defSecondary.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);

        TransactionStatus statusPrimary = primaryTransactionManager.getTransaction(defPrimary);
        TransactionStatus statusSecondary = secondaryTransactionManager.getTransaction(defSecondary);

        try {


            // 执行主数据源的逻辑
            CustomerDO customerDO = new CustomerDO();
            customerDO.setName("拉拉");
            customerDO.setEmail("example@example.com");
            customerMapper.insert(customerDO); // 伪代码，执行插入操作

            if (1 ==1 ) {
                throw new RuntimeException("dddddddd");
            }

            // 执行次数据源的逻辑
            User user = new User();
            user.setUserName("李洪伟");
            user.setAge(20);
            userMapper.insert(user); // 伪代码，执行插入操作

            // 如果一切顺利，则提交两个事务
            primaryTransactionManager.commit(statusPrimary);
            secondaryTransactionManager.commit(statusSecondary);
        } catch (Exception ex) {
            // 如果出现异常，回滚两个事务
            primaryTransactionManager.rollback(statusPrimary);
            secondaryTransactionManager.rollback(statusSecondary);
            throw ex; // 抛出异常，确保调用者知道操作失败
        }


        return null;
    }
}
